package fr.miage.ageco.web.beans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.RowEditEvent;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import fr.miage.ageco.dataaccess.bo.Employe;
import fr.miage.ageco.dataaccess.services.EmployeService;

@ViewScoped
@ManagedBean
public class EmployeBean implements Serializable {

	/**
     * 
     */
	private static final long serialVersionUID = 8492638758809491947L;

	private Logger logger = Logger.getLogger(EmployeBean.class); 
	EmployeService employeService;

	private List<Employe> listEmploye;
	private List<Employe> filteredEmploye;
	private Employe selectedEmploye;
	

	private int id;
	private String nom;
	private String prenom;
	private String naissance;
	private String email;
	private String fonction;
	private String agence;
	private Double etp;

	public EmployeBean() {

	}

	@PostConstruct
	public void init() {
		listEmploye = getAllEmploye();
		logger.info("Initialisation de la liste des employés");
	}

	// Méthodes pour l'IHM
	
	public List<Employe> getAllEmploye() {
		return employeService.getAllEmploye();
	}

	// Ajout
	public String insertEmploye() {
		logger.info("début Insertion de l'employé");
		Employe employe = new Employe();

		// getNom() pour récupérer le nom saisi par l'utilisateur dans le champ
		employe.setNom(getNom());
		employe.setPrenom(getPrenom());
		employe.setNaissance(getNaissance());
		employe.setEmail(getEmail());
		employe.setFonction(getFonction());
		employe.setAgence(getAgence());
		employe.setEtp(getEtp());

		// Ajout dans la BD
		employeService.insertEmploye(employe);
		logger.info("Insertion de l'employé n° " + employe.getId() + "");

		// Rechargement de la liste
		listEmploye = getAllEmploye();
		logger.info("Rechargement de la liste des employés");
		
		FacesMessage msg = new FacesMessage("Employé ajouté : ",
				(employe.getNom() + " " + employe.getPrenom()));
		FacesContext.getCurrentInstance().addMessage(null, msg);
		return "";
	}

	// Modification
	public void onEdit(RowEditEvent event) {
		logger.info("edition del'employe");
		FacesMessage msg = new FacesMessage("Employé édité :",
				((Employe) event.getObject()).getId() + "");
		// Modification de l'objet en base
		employeService.updateEmploye((Employe) (event.getObject()));
		logger.info("Mise à jour de l'employé n° : " + ((Employe) event.getObject()).getId() + "");
		// Rechargement de la liste
		listEmploye = getAllEmploye();
		logger.info("Rechargement de la liste");
		
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	// suppression
	public void onCancel(RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Employé supprimé :",
				((Employe) event.getObject()).getId()+"");
		//suppression dans le tableau
		listEmploye.remove((Employe) event.getObject());
		//suppression en base
		employeService.deleteEmploye((Employe) event.getObject());
		logger.info("Suppression de l'employé n° : " + ((Employe) event.getObject() + "") );
		
		listEmploye = getAllEmploye();
		logger.info("Rechargement de la liste");
		//Affichage du message
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	
	// Sélection d'une ligne
	public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Employé ", ((Employe) event.getObject()).getId() + " sélectionné"); 
       // selectedEmploye = (Employe) event.getObject();
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
	
	// Déselection de la ligne
    public void onRowUnselect(UnselectEvent event) {  
        FacesMessage msg = new FacesMessage("Employé ", ((Employe) event.getObject()).getId() + " desélectionné");  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  

	// Getters & setters

	public String getNom() {
		return nom;
	}

	public EmployeService getEmployeService() {
		return employeService;
	}

	public void setEmployeService(EmployeService employeService) {
		this.employeService = employeService;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getNaissance() {
		return naissance;
	}

	public void setNaissance(String naissance) {
		this.naissance = naissance;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getFonction() {
		return fonction;
	}

	public void setFonction(String fonction) {
		this.fonction = fonction;
	}

	public String getAgence() {
		return agence;
	}

	public void setAgence(String agence) {
		this.agence = agence;
	}

	public Double getEtp() {
		return etp;
	}

	public void setEtp(Double etp) {
		this.etp = etp;
	}

	public List<Employe> getListEmploye() {
		return listEmploye;
	}

	public void setListEmploye(List<Employe> listEmploye) {
		this.listEmploye = listEmploye;
	}

	public List<Employe> getFilteredEmploye() {
		return filteredEmploye;
	}

	public void setFilteredEmploye(List<Employe> filteredEmploye) {
		this.filteredEmploye = filteredEmploye;
	}

	public Employe getSelectedEmploye() {
		return selectedEmploye;
	}

	public void setSelectedEmploye(Employe selectedEmploye) {
		this.selectedEmploye = selectedEmploye;
	}

}
